﻿using MVC_Project.BLL;
using Project.DAL;
using Project.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project.BLL
{
    public class RolesBll : BaseBll<Roles>
    {
        public override void SetDal()
        {
            Dal = new RolesDal();
        }


        public List<Roles> GetRolesByPage(string name, int page, int size, out int count)
        {
            var roles = base.Query();

            if (!string.IsNullOrWhiteSpace(name))
            {
                roles = roles.Where(r => r.RName.Contains(name));
            }
            count = roles.Count();

            var data = roles.OrderBy(r => r.RId).Skip((page - 1) * size).Take(size);

            return data.ToList();
        }

        public bool UpdateRoles(Roles users)
        {
            var user = Dal.Query().Where(u => u.RId == users.RId).FirstOrDefault();
            if (user != null)
            {
                user.RName = users.RName;
                user.Type = users.Type;
                user.Remark = users.Remark;
                user.LastTime = users.LastTime;
                user.DId = users.DId;

                Dal.Update(user);

                return Dal.SaveChanges();
            }
            else
            {
                return false;
            }
        }

        public bool DeleteRoles(int CId)
        {
            var data = Dal.Query().Where(r => r.RId == CId).FirstOrDefault();
            if (data != null)
            {
                Dal.Delete(data);
                return Dal.SaveChanges();
            }
            else
            {
                return false;
            }
        }

        public bool DeleteRoles(int[] CId)
        {
            var list = Dal.Query().Where(r => CId.Contains(r.RId)).ToList();
            if (list != null)
            {
                Dal.Delete(list);
                return Dal.SaveChanges();
            }
            else
            {
                return false;
            }
        }
    }
}
